package com.nan.leetcode.剑指offer第二版.反转链表;


import java.util.Stack;

public class Test {
    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
        }
    }
    public ListNode reverseList(ListNode head) {
        if(head==null){
            return null;
        }
        Stack<Integer> stack=new Stack<>();
        while(head!=null){
            stack.push(head.val);
            head=head.next;
        }
        ListNode head1=new ListNode(stack.pop());
        ListNode last=head1;
        while(!stack.isEmpty()){
            last.next=new ListNode(stack.pop());
            last=last.next;
        }
        return head1;
    }
}
